package com.yy.service.ForegroundService.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.yy.config.R;
import com.yy.mapper.ForegroundMapper.UserMapper;
import com.yy.domain.Account;
import com.yy.domain.User;
import com.yy.service.ForegroundService.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper usermapper;
    @Override
    public R toLogin(Account account) {
        if (account.getUsername() == null || account.getPassword() == null) {
            return R.fail("账号或密码不能为空");
        }
        //查询账号
        Account accountDB = usermapper.selectAccount(account);
        if (accountDB == null){
            return R.fail("账号不存在");
        }
        // 取查询出的账号盐值
        String salt = accountDB.getSalt();
        // 用户输入的原密码加盐
        String saltedInputPassword  = account.getPassword() + salt;
        // 对加盐后的用户输入密码进行加密
        String md5password = SecureUtil.md5(saltedInputPassword);
        // 获取查询出账号加密后的密码（数据库中存储的该账号加密密码）
        String passwordInDB = accountDB.getPassword();
        // 将用户输入密码加盐加密后与数据库密码对比
        if (md5password.equals(passwordInDB)) {
            int userid = accountDB.getUserid();
            StpUtil.login(userid);
            String tokenValue = StpUtil.getTokenValue();
            //将账号信息返回
            User user = usermapper.selectUserById(userid);
            user.setPassword(null);
            System.out.println("登录成功-------------------------------" );
            System.out.println("登录成功" + StpUtil.isLogin());
            return R.ok("登录成功",user);
        } else {
            return R.fail("登录失败，密码错误");
        }
    }
    @Override
    public User selectUserById(int id) {
        if(id == 0){
            return null;
        }
        return usermapper.selectUserById(id);
    }
    @Override
    public int updateUserById(User user) {
        if(user.getId() == 0){
            return -1;
        }
        if (user.getPassword() != null){
            return usermapper.updatePassword(user);
        }else {
            return usermapper.updateUserById(user);
        }
    }
    @Override
    public int selectCountById(Integer id) {
        if(id == null){
            return -1;
        }
        return usermapper.selectCountById(id);
    }
    @Override
    public int deleteUserById(Integer id) {
        usermapper.deleteAccById(id);
        return usermapper.deleteUserById(id);
    }
    @Override
    public int restAccountById(Integer id) {
        return usermapper.restAccountById(id);
    }
    @Override
    public String insertUserInfo(User user) {
        String username = user.getUsername();
        Account account = new Account();
        account.setUsername(username);
        Account account1 = usermapper.selectAccount(account);
        if (account1 != null){
            return "账号已存在";
        }
        usermapper.insertUserInfo(user);
        return String.valueOf(user.getId());
    }
    @Override
    public int insertUserToAccount(Account account) {
        String md5salt = RandomUtil.randomString(16);
        account.setSalt(md5salt);
        String saltpassword = account.getPassword() + md5salt;
        String md5Password = SecureUtil.md5(saltpassword);
        account.setPassword(md5Password);
        return usermapper.insertUserToAccount(account);
    }
    @Override
    public int selectCartCountById(int id) {
        return usermapper.selectCartCountById(id);
    }
    @Override
    public int selectCommentById(int id) {
        return usermapper.selectCommentById(id);
    }
    @Override
    public Account selectAccountById(int id) {
        return usermapper.selectAccountById(id);
    }
    @Override
    public Object queryUser(Account account) {
        Account account1 = usermapper.selectAccount(account);
        if (account1 != null){
            return "账号已存在";
        }
        return null;
    }
    @Override
    public Boolean outLogin(String userId) {
        //登出
        StpUtil.logout(userId);
        //检查登录状态
        return StpUtil.isLogin();
    }

    @Override
    public User findUserByEmail(String email) {
        return usermapper.findUserByEmail(email);
    }

    @Override
    public Account findUserByUsername(String username) {
        return usermapper.findUserByUsername(username);
    }

    @Override
    public Boolean updateUserAccountUser(Account user) {

        return usermapper.updateAccountById(user);
    }

    @Override
    public User selectUserBymail(String mailTo) {
        return usermapper.selectUserBymail(mailTo);
    }

    @Override
    public Account selectAccountByUserName(String username) {
        return usermapper.selectAccountByUserName(username);
    }

}
